/*
 * ValidadorFecha.java
 *
 * Created on October 24, 2007, 4:57 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
package web.util.validadores;

import com.sun.rave.web.ui.model.UploadedFile;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;
import web.util.Utils;

/**
 *
 * @author kabad
 */
public class ValidadorFileUpload implements Validator {
    private Long max_size ; //new Long(com.sun.rave.web.ui.util.UploadFilter.MAX_SIZE);

    /** Creates a new instance of ValidadorFecha */
    public ValidadorFileUpload() {
        this.max_size=4000000L;
 }

    @Override
    public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
        FacesMessage mensajeFace = null;
        String keyUpload = "";
        String mensaje = "";
        UploadedFile uploadedFile = (UploadedFile) value;
        try {
            if (uploadedFile == null) {
                mensajeFace = new FacesMessage(FacesMessage.SEVERITY_ERROR, "El campo es requerido", "");
                throw new ValidatorException(mensajeFace);
            }
            String uploadedFileName = uploadedFile.getOriginalName();
            // Some browsers return complete path name, some don't
            // Make sure we only have the file name
            // First, try forward slash
            int index = uploadedFileName.lastIndexOf('/');
            String justFileName;
            if (index >= 0) {
                justFileName = uploadedFileName.substring(index + 1);
            } else {
                // Try backslash
                index = uploadedFileName.lastIndexOf('\\');
                if (index >= 0) {
                    justFileName = uploadedFileName.substring(index + 1);
                } else {
                    // No forward or back slashes
                    justFileName = uploadedFileName;
                }
            }

            String tiposPermitidos = "doc, xls, ppt, pdf, odt, jpg, gif, png, zip, gzip, rar, tar";
            String uploadedFileType = uploadedFile.getContentType();
            boolean esTipoCorrecto = uploadedFileType.equals("image/jpeg") || uploadedFileType.equals("image/pjpeg") ||
                    uploadedFileType.equals("image/gif") || uploadedFileType.equals("image/png") || uploadedFileType.equals("image/x-png") || uploadedFileType.equals("application/msword") || uploadedFileType.equals("application/vnd.ms-excel") ||
                    uploadedFileType.equals("application/vnd.ms-powerpoint") || uploadedFileType.equals("application/pdf") || uploadedFileType.equals("application/zip") ||
                    uploadedFileType.equals("application/x-zip-compressed") || uploadedFileType.equals("application/octet-stream") ||
                    uploadedFileType.equals("application/x-rar-compressed") || uploadedFileType.equals("application/x-gzip") ||
                    uploadedFileType.equals("application/x-gzip-compressed") || uploadedFileType.equals("application/x-tar") ||
                    uploadedFileType.equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document") ||
                    uploadedFileType.equals("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") ||
                    uploadedFileType.equals("application/vnd.openxmlformats-officedocument.presentationml.presentation") ||
                    uploadedFileType.equals("application/vnd.oasis.opendocument.text");

            if (!esTipoCorrecto) {
                keyUpload = "component.fileUpload.tipoArchivo";
                String[] parametros = {justFileName, tiposPermitidos};
                mensaje = Utils.getMessageResourceString(context.getApplication().getMessageBundle(), keyUpload, parametros, context.getViewRoot().getLocale());
                mensajeFace = new FacesMessage(FacesMessage.SEVERITY_ERROR, mensaje, "");
                throw new ValidatorException(mensajeFace);
            }
            
//            if (uploadedFile.getSize()>this.max_size) {
//                keyUpload = "org.apache.myfaces.FileUpload.SIZE_LIMIT";
//                String[] parametros = {justFileName, this.max_size.toString()};
//                mensaje = Utils.getMessageResourceString(context.getApplication().getMessageBundle(), keyUpload, parametros, context.getViewRoot().getLocale());
//                mensajeFace = new FacesMessage(FacesMessage.SEVERITY_ERROR, mensaje, "");
//                throw new ValidatorException(mensajeFace);
//
//            }
        } catch (ClassCastException ex) {
            mensajeFace = new FacesMessage(FacesMessage.SEVERITY_ERROR, "No es posible adjuntar el archivo", "");
            throw new ValidatorException(mensajeFace);
        }
    }
}
